package com.story.modular.app.service.impl;

import com.story.common.persistence.dao.UserMapper;
import com.story.common.persistence.dao.VedioDetailMapper;
import com.story.common.persistence.model.User;
import com.story.common.persistence.model.VedioDetail;
import com.story.core.shiro.ShiroKit;
import com.story.core.support.BeanKit;
import com.story.modular.app.dao.CateVedioDao;
import com.story.modular.app.dao.VedioAgeDao;
import com.story.modular.app.dao.VedioCategoryDao;
import com.story.modular.app.dao.VedioDetailDao;
import com.story.modular.app.service.VedioService;
import com.story.modular.app.warpper.ResultVo;
import com.story.modular.app.warpper.VedioDto;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * @author Administrator
 * @decription
 * @Date 2017-12-25 16:57
 */
@Service
public class VedioServiceImpl implements VedioService{

    @Autowired
    private VedioDetailDao vedioDetailDao;

    @Autowired
    private VedioCategoryDao vedioCategoryDao;

    @Autowired
    private VedioDetailMapper vedioDetailMapper;

    @Autowired
    private UserMapper userMapper;

    @Autowired
    private VedioAgeDao vedioAgeDao;

    @Autowired
    private CateVedioDao cateVedioDao;

    @Override
    @Transactional(rollbackForClassName = "BussinessException.class")
    public ResultVo excelBatchAdd(List<VedioDto> vedioDtoList) {
        List<VedioDetail> vedioDetailList = new ArrayList<VedioDetail>();
        List<Integer> cateIdList = new ArrayList<>();
        List<Integer> ageTypeList = new ArrayList<>();
        for (VedioDto vedioDto : vedioDtoList) {
            VedioDetail vedioDetail = new VedioDetail();
            BeanKit.copyProperties(vedioDto,vedioDetail);
            //设置年龄默认
            String[] ageTypeArray = vedioDto.getAgeType().split(",");

            for (String ageTypeStr : ageTypeArray) {
                ageTypeList.add(Integer.parseInt(ageTypeStr));
            }
            //设置推荐指数默认
            if(vedioDetail.getRating() == null || vedioDetail.getRating() > 5 || vedioDetail.getRating() < 1) {
                vedioDetail.setRating(5);
            }
            //判断名字是否与数据库中重名
            int count  = vedioDetailDao.countByName(vedioDetail.getName().trim());
            if(count > 0) {
                vedioDetailList.clear();
                return new ResultVo(500,vedioDetail.getName()+" 与数据库中重复,请修改后重新上传");
            }
            //所属专辑名称填写有误
            String[] cateNameArray = vedioDto.getCategoryName().split(",");
            for(String cateName : cateNameArray) {
                Integer cateId = vedioCategoryDao.getIdByName(vedioDto.getCategoryName().trim());
                if(cateId == null) {
                    return new ResultVo(500,"不存在 "+ vedioDto.getCategoryName() + " 这个专辑");
                } else {
                    cateIdList.add(cateId);
                }
            }

            vedioDetail.setCreateTime(new Date());
            vedioDetailList.add(vedioDetail);
        }
        for (VedioDetail vedioDetail : vedioDetailList) {
            Integer id = ShiroKit.getUser().getId();
            User user = userMapper.selectById(id);
            vedioDetail.setUploader(user.getName());
            vedioDetailMapper.insert(vedioDetail);
            vedioAgeDao.batchInsertAgeType(ageTypeList,vedioDetail.getId());
            cateVedioDao.batchInsertCateVedioId(cateIdList,vedioDetail.getId());
        }
        return new ResultVo(200,"新增成功");
    }
}
